/*
Kiwi Editor is a framework for building WYSIWYG web editors.
Copyright (C) 2011  Roosevelt de Lima Sardinha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact the author at roosevelt1@gmail.com (Roosevelt) 
*/

/**
 * A workspace manager may create and remove workspaces.
 * It's also possible to recover or load a workspace previously saved.
 * It's role is to manage the existing workspaces.
 * @author Roosevelt de Lima Sardinha
 * @constructor
 */
function workspaceManager(){
	
	/**
	 * Creates a workspace.
	 * @returns The workspace that was created.
	 * @type {Object}
	 */
	this.createWorkspace = function(){
		var ws = new workspace();
		return ws;		
	}
	
	/**
	 * Returns a workspace by its id.
	 * @param {String} id The id of the workspace
	 * @return The workspace that has the given id
	 * @type {Object} 
	 */
	this.get = function(id){
		var jsonStr = "";
		// Returns a workspace		
		var myRequest = new request();
        myRequest.sendSincRequest('GET', null, "workspaces/" + id, function(response){
            jsonStr = response;
        });
		var json = JSON.parse(jsonStr);
		var ws = new workspace();
		
		// Fill workspace properties
		ws.id = json['id'];
		ws.activeProfile = json['current_profile_id'];
		ws.profiles = json['profiles'];
		ws.editionAreas = json['edition_areas'];
		ws.instructionBoxes = json['instruction_boxes'];
		ws.propertyBoxes = json['property_boxes'];
		ws.toolBoxes = json['tool_boxes'];
		
		return ws;
	}
	
	/**
	 * Returns all existing workspaces.
	 * @return An object containing all the workspaces.
	 * @type {Object}
	 */
	this.all = function(){
		var jsonStr = "";
		// Returns all workspaces		
		var myRequest = new request();
        myRequest.sendSincRequest('GET', null, "workspaces", function(response){
            jsonStr = response;
        });
		return JSON.parse(jsonStr);
	}
	
	/**
	 * Removes a workspace by its id.
	 * @param {String} id The id of the workspace to remove
	 */
	this.remove = function(id){
		// Removes a workspace by id
		var myRequest = new request();
		var params = [];
		params['_method'] = 'DELETE';
        myRequest.sendSincRequest('POST', params, "workspaces/" + id, function(response){});
	}
	
	/**
	 * Loads an existing worspace by a given id
	 * @param {String} id The id of the workspace to load
	 * @return The workspace that was loaded
	 * @type {Object}
	 */
	this.loadWorkspace = function(id){
		var wsJSON = this.get(id);
		var ws = this.createWorkspace();
		
		// Loading Toolboxes
		var toolboxJSON = null;
		var toolbox = null;
		for (var i = 0; i < wsJSON.toolBoxes.length; i++) {
			toolboxJSON = wsJSON.toolBoxes[i]; 
			toolbox = ws.createToolBox(toolboxJSON.id, {
				left: toolboxJSON.left,
				top: toolboxJSON.top,
				width: toolboxJSON.width,
				height: toolboxJSON.height,
				parent: toolboxJSON.parent,
				title: toolboxJSON.title,
				cssPath: toolboxJSON.css_path
			});
			
			if (wsJSON.activeProfile) {
				ws.setActiveProfile(wsJSON.activeProfile, toolbox.id, {					
					// TODO: after creating tool object, remove this hard-coded thing, and complete with each tool properties
					propertyBox: 'propertyBox',
					instructionBox: 'instructionBox'
				});
			}
		}
		
		// Loading Property Boxes
		var propertyBoxJSON = null;
		for (var i = 0; i < wsJSON.propertyBoxes.length; i++) {
			propertyBoxJSON =  wsJSON.propertyBoxes[i];
			ws.createPropertyBox(propertyBoxJSON.id, {
				left: propertyBoxJSON.left,
				top: propertyBoxJSON.top,
				width: propertyBoxJSON.width,
				height: propertyBoxJSON.height,
				parent: propertyBoxJSON.parent,
				title: propertyBoxJSON.title,
				cssPath: propertyBoxJSON.css_path
			});
		}
		
		// Loading instruction boxes
		var instructionBoxJSON = null;
		for (var i = 0; i < wsJSON.instructionBoxes.length; i++) {
			instructionBoxJSON = wsJSON.instructionBoxes[i]; 
			ws.createInstructionBox(instructionBoxJSON.id, {
				left: instructionBoxJSON.left,
				top: instructionBoxJSON.top,
				width: instructionBoxJSON.width,
				height: instructionBoxJSON.height,
				parent: instructionBoxJSON.parent,
				title: instructionBoxJSON.title,
				cssPath: instructionBoxJSON.css_path
			});
		}
	    
		// Loading edition areas
		var editionAreaJSON = null;
		for (var i = 0; i < wsJSON.editionAreas.length; i++) {
			editionAreaJSON = wsJSON.editionAreas[i]; 
			ws.createEditionArea(editionAreaJSON.id, {
				left: editionAreaJSON.left,
				top: editionAreaJSON.top,
				width: editionAreaJSON.width,
				height: editionAreaJSON.height,
				parent: editionAreaJSON.parent,
				title: editionAreaJSON.title,
				cssPath: editionAreaJSON.css_path
			});
		}
		
		return ws;
	}
}
